//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmHestonOp.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Processes/HestonProcess.h>
#include <gen/QL/Experimental/Finitedifferences/FdmQuantoHelper.h>
#include <gen/QL/Experimental/Finitedifferences/FdmLinearOpComposite.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::CFdmHestonOp (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Cephei::QL::Processes::IHestonProcess^ hestonProcess, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Experimental::Finitedifferences::IFdmQuantoHelper^>^ quantoHelper) : CFdmLinearOpComposite(CFdmHestonOp::typeid)
{
    CFdmMesher^ _Cmesher;
    CHestonProcess^ _ChestonProcess;
    CFdmQuantoHelper^ _CquantoHelper;
    try
    {
#ifdef HANDLE
        _phFdmHestonOp = NULL;
#endif
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _ChestonProcess = safe_cast<CHestonProcess^> (hestonProcess);
        _ChestonProcess->Lock();
        boost::shared_ptr<QuantLib::HestonProcess>& _hestonProcess = static_cast<boost::shared_ptr<QuantLib::HestonProcess>&> (_ChestonProcess->GetShared ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Experimental::Finitedifferences::IFdmQuantoHelper^>::IsSome::get (quantoHelper))
        {
            _CquantoHelper = safe_cast<CFdmQuantoHelper^> (quantoHelper->Value);
            _CquantoHelper->Lock();
        }
        boost::shared_ptr<QuantLib::FdmQuantoHelper>& _quantoHelper = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Experimental::Finitedifferences::IFdmQuantoHelper^>::IsSome::get (quantoHelper) ? static_cast<boost::shared_ptr<QuantLib::FdmQuantoHelper>&> (_CquantoHelper->GetShared ()) : boost::shared_ptr<QuantLib::FdmQuantoHelper>()); //1
        _ppFdmHestonOp = new boost::shared_ptr<QuantLib::FdmHestonOp> (new QuantLib::FdmHestonOp ( _mesher,  _hestonProcess,  _quantoHelper ));
        SetFdmLinearOpComposite (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_ChestonProcess != nullptr) _ChestonProcess->Unlock();
        if (_CquantoHelper != nullptr) _CquantoHelper->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::CFdmHestonOp (boost::shared_ptr<QuantLib::FdmHestonOp>& childNative, Object^ owner) : CFdmLinearOpComposite(CFdmHestonOp::typeid)
{
#ifdef HANDLE
	_phFdmHestonOp = NULL;
#endif
	_ppFdmHestonOp = &childNative;
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonOp));
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::CFdmHestonOp (QuantLib::FdmHestonOp& childNative, Object^ owner) : CFdmLinearOpComposite(CFdmHestonOp::typeid)
{
#ifdef HANDLE
	_phFdmHestonOp = NULL;
#endif
	_ppFdmHestonOp = new boost::shared_ptr<QuantLib::FdmHestonOp> (&childNative);
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonOp));
    _FdmHestonOpOwner = owner;
    _FdmLinearOpCompositeOwner = owner;
}

Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::CFdmHestonOp (CFdmHestonOp^ copy) : CFdmLinearOpComposite(CFdmHestonOp::typeid)
{
#ifdef HANDLE
	_phFdmHestonOp = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmHestonOp = new boost::shared_ptr<QuantLib::FdmHestonOp> (copy->GetShared());
        _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonOp));
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::CFdmHestonOp (System::Type^ t) : CFdmLinearOpComposite(CFdmHestonOp::typeid)
{
#ifdef HANDLE
	_phFdmHestonOp = NULL;
#endif
	if (!t->IsSubclassOf(CFdmHestonOp::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::CFdmHestonOp (QuantLib::Handle<QuantLib::FdmHestonOp>& childNative, Object^ owner)  : CFdmLinearOpComposite(CFdmHestonOp::typeid)
{
	_phFdmHestonOp = &childNative;
	_ppFdmHestonOp = &static_cast<boost::shared_ptr<QuantLib::FdmHestonOp>>(childNative.currentLink());
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonOp));
    _FdmHestonOpOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::CFdmHestonOp (QuantLib::Handle<QuantLib::FdmHestonOp> childNative)  : CFdmLinearOpComposite(CFdmHestonOp::typeid)
{
	_phFdmHestonOp = &childNative;
	_ppFdmHestonOp = &static_cast<boost::shared_ptr<QuantLib::FdmHestonOp>>(childNative.currentLink());
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonOp));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::CFdmHestonOp (QuantLib::FdmHestonOp childNative)  : CFdmLinearOpComposite(CFdmHestonOp::typeid)
{
#ifdef HANDLE
	_phFdmHestonOp = NULL;
#endif
	_ppFdmHestonOp = new boost::shared_ptr<QuantLib::FdmHestonOp> (new QuantLib::FdmHestonOp (childNative));
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonOp));
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::~CFdmHestonOp ()
{
    if (_ppFdmHestonOp != NULL)
    {
	    delete _ppFdmHestonOp;
        _ppFdmHestonOp = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::!CFdmHestonOp ()
{
    if (_ppFdmHestonOp != NULL)
    {
	    delete _ppFdmHestonOp;
    }
}
QuantLib::FdmHestonOp& Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::GetReference ()
{
    if (_ppFdmHestonOp == NULL) throw gcnew NativeNullException ();
	return **_ppFdmHestonOp;
}
boost::shared_ptr<QuantLib::FdmHestonOp>& Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::GetShared ()
{
    if (_ppFdmHestonOp == NULL) throw gcnew NativeNullException ();
	return *_ppFdmHestonOp;
}
QuantLib::FdmHestonOp* Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::GetPointer ()
{
    if (_ppFdmHestonOp == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmHestonOp;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmHestonOp>& Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::GetHandle ()
{
	if (_phFdmHestonOp == NULL)
	{
		_phFdmHestonOp = new Handle<QuantLib::FdmHestonOp> (*_ppFdmHestonOp);
	}
	return *_phFdmHestonOp;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::HasNative () 
{
	return (_ppFdmHestonOp != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdmHestonOp^ Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::SetTime (Double t1, Double t2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1);
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2);
    	(*_ppFdmHestonOp)->setTime ( _t1,  _t2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdmHestonOp)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmHestonOp^ Cephei::QL::Experimental::Finitedifferences::CFdmHestonOp_Factory::Create (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Cephei::QL::Processes::IHestonProcess^ hestonProcess, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Experimental::Finitedifferences::IFdmQuantoHelper^>^ quantoHelper)
{
    return gcnew CFdmHestonOp ( mesher,  hestonProcess,  quantoHelper);
}
